﻿using System;
using System.Xml;
using System.Text;
using System.IO;
namespace CodeCounsel.Office.WordSourceViewer
{
    class Transform
    {
        static XmlNamespaceManager NamespaceManager = null;
        const string XPath = "//pkg:part[@pkg:contentType='application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml']/pkg:xmlData/w:document/w:body";

        static Transform()
        {
            NamespaceManager = new XmlNamespaceManager(
                new NameTable());
            NamespaceManager.AddNamespace(
                "pkg", "http://schemas.microsoft.com/office/2006/xmlPackage");
            NamespaceManager.AddNamespace(
                "w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
        }

        public static string ConvertToContentString(string wordprocessingML)
        {
            StringBuilder response = new StringBuilder();
            try
            {
                XmlDocument document = new XmlDocument();
                document.LoadXml(wordprocessingML);
                XmlNode content = null;
                if (Globals.ThisAddIn.ShowFullSource == false)
                {
                    content = document.SelectSingleNode(XPath, NamespaceManager);
                    if (content.ChildNodes.Count > 0)
                    {
                        content.RemoveChild(content.LastChild);
                    }
                }
                else
                {
                    content = document.DocumentElement;
                }
                using (XmlTextWriter writer = new XmlTextWriter(
                    new StringWriter(response)))
                {
                    writer.Formatting = Formatting.Indented;
                    content.WriteContentTo(writer);
                }
            }
            catch(Exception e)
            {
                response.Append("Error " + e.Message);
            }
            return response.ToString();
        }

        internal static string ConvertToWordprocessingMLDocument(string fragment)
        {
            StringBuilder builder = new StringBuilder();
            XmlDocument document = new XmlDocument();
            try
            {
                if (Globals.ThisAddIn.ShowFullSource == false)
                {
                    document.LoadXml(Properties.Resources.Sample);
                    XmlNode body = document.SelectSingleNode(XPath, NamespaceManager);
                    body.InnerXml = fragment;
                    using (StringWriter writer = new StringWriter(builder))
                    {
                        document.Save(writer);
                    }
                }
                else
                {
                    builder.Append("<pkg:package xmlns:pkg=\"" + NamespaceManager.LookupNamespace("pkg") + "\">");
                    builder.Append(fragment);
                    builder.Append("</pkg:package>");
                }
            }
            catch(Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Error in the XML: " + e.Message);
            }
            return builder.ToString();
        }
    }
}
